# --*-- coding:utf-8 --*--

import sys
sys.path.append("..")
from multiprocessing import Pool
from multiprocessing.dummy import Pool
import requests
import re
import urllib
import warnings
warnings.filterwarnings("ignore")
import time
import random
from urllib.parse import quote
import datetime
# reload(sys)
# sys.setdefaultencoding("utf8")



class Get_data(object):

	cookies = None
	proxies = None

	'''获取关键词在线商品数和成交数，竞争度 = 搜索人气/在线商品数'''

	def get_sale(self,data_list):

		keyword_list = data_list[0]
		reserve_price = data_list[1]
		url = 'https://s.taobao.com/search'
		# 有价格区间的参数
		if reserve_price != '':  # 没有价格区间记得传''
			data_value = 'reserve_price' + urllib.parse.quote_plus(str(reserve_price))
			payload = {	'data-key':'filter',  #sort
						'data-value':data_value,  # sale-desc价格为空的时候参数
						'ajax':'true',
						'_ksTS':'1526608409915_1139',
						'callback':'jsonp1140',
						'q':keyword_list,
						'imgfile':'',
						'commend':'all',
						'ssid':'s5-e',
						'search_type':'item',
						'sourceId':'tb.index',
						'spm':'a21bo.2017.201856-taobao-item.1',
						'stats_click':'search_radio_all:1',
						'initiative_id':'staobaoz_20180518',
						'sort':'sale-desc',
						'ie':'utf8'}

		else:
			# 价格区间为空的的参数
			payload = {	'data-key':'sort',  #sort
						'data-value':'sale-desc',  # sale-desc价格为空的时候参数
						'ajax':'true',
						'_ksTS':'1526608409915_1139',
						'callback':'jsonp1140',
						'q':keyword_list,
						'imgfile':'',
						'js':'1',
						'stats_click':'search_radio_all:1',
						'initiative_id':'staobaoz_20180518',
						'ie':'utf8'}
		i = 0
		while True:
			resp = requests.get(url,params = payload,verify=False,proxies = self.proxies)
			# resp.encoding = 'utf-8'
			try:
				sale = str(re.findall(r'"view_sales":"([^"]+)"',resp.text)[0])[:-9]
				return sale  #返回每个关键字最高销量的集合
			except:
				# print '失败重新爬取'
				i = i + 1
				if i > 4:
					return ' '
				# time.sleep(random.randint(1,3))



	def get_onlineGoods(self,data_list):


		########  时间获取
		current_time = (str(datetime.datetime.now() - datetime.timedelta(days = 1)).split(' ')[0])
		pro_time = (str(datetime.datetime.now() - datetime.timedelta(days = 30)).split(' ')[0])
		# print (current_time,pro_time)

		# 还要传最近1或7天时间数据
		url = 'https://sycm.taobao.com/mc/searchword/relatedWord.json?dateRange='+pro_time+'%7C'+current_time+'&dateType=recent30&pageSize=10&page=1&order=desc&orderBy=seIpvUvHits&keyword='+data_list+'&device=0&indexCode=seIpvUvHits%2CsePvIndex%2CclickRate%2CclickHits%2CclickHot&_=1537520061497&token='
		# url = 'https://sycm.taobao.com/mc/searchword/relatedWord.json?dateRange=2018-08-21%7C2018-09-19&dateType=recent30&pageSize=10&page=1&order=desc&orderBy=seIpvUvHits&keyword=%E5%B8%BD%E5%AD%90%E5%A5%B3&device=0&indexCode=seIpvUvHits%2CsePvIndex%2CclickRate%2CclickHits%2CclickHot&_=1537520061497&token='
		# 读取cookie
		
		# # cookie = {'tracknick': '', 't': 'aea0450cbb9081a5351d78c2b7346bc2', 'cookie2': '1fbb0686c8b2c3b47241d44b913b9c89', 'uc1': 'cookie14=UoTfKf4YdyMETg%3D%3D&lng=zh_CN', 'uc3': 'id2=&nk2=&lg2=', 'skt': 'b58db241927fa6fa', '_tb_token_': '634980151535', 'unb': '3416683071', 'x': '2878767477', 'sn': 'chailtee%E9%A6%A8%E7%BC%87%E5%90%89%E5%AE%B6%E5%B1%85%E6%97%97%E8%88%B0%E5%BA%97%3A%E8%89%BA%E9%A3%8E', 'isg': 'BJGRycOKrv6CmcLqfn8QOB6FrJ0r_gVwOmrMFHMmjdh3GrFsu04VQD_4uGj8CZ2o', 'apush481ea92b828a3c9ce9ed7c72f58bb0ea': '%7B%22ts%22%3A1532927426044%2C%22parentId%22%3A1532927420094%7D', 'csg': '75203000', 'v': '0', 'cna': 'QKnNExvNvVQCAW+s57KFKXBm'}
		# keyword_list = data_list
		# aa = quote(keyword_list)
		# k = 0
		# url = 'https://sycm.taobao.com/mq/searchword/relatedWord.json?'
		# payload = {}
		# for i in aa :
		# 	url = url + str(k) + '=' + i + '&'
		# 	k = k + 1
		# url = url + 'dateRange='+pro_time+'%7C'+current_time+'&dateType=recent7&device=0&keyword='+keyword_list+'&token=7a824665b&_=1531549878076'
		headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
				   'Host':'sycm.taobao.com',
				   'Accept': '*/*'
				   }

		time.sleep(random.uniform(1,40)) # 1-30秒随机请求生意参谋数据
		resp = requests.get(url,verify=False,headers = headers,cookies = self.cookies,proxies = self.proxies)
		data_dict = resp.json()
		# return data_dict
		keyword_info = []
		for i in data_dict['data']:
			keyword_set = {}
			keyword_set["keyword"] = i['keyword']
			try:
				keyword_set["suv"] = i['seIpvUvHits']  #搜索人气
			except:
				keyword_set["suv"] = '-'

			try:
				keyword_set["shv"] = i['sePvIndex']  # 搜索热度
			except:
				keyword_set["shv"] = '-'

			try:
				keyword_set['clickRate'] = "%.2f%%" % (i['clickRate'] * 100) # 点击率
			except:
				keyword_set['clickRate'] = '-'

			try:
				keyword_set["tradeIndex"] = i['tradeIndex'] # 交易指数
				# tradeIndex = i['tradeIndex'] # 交易指数
			except:
				keyword_set["tradeIndex"] = '-'
				# tradeIndex = '-'

			try:
				keyword_set["p4pAmt"] = round(i['p4pAmt'],2) #直通车参考价
			except:
				keyword_set["p4pAmt"] = '-'

			try:
				keyword_set["clickUv"] = i['clickHits'] # 点击人气
			except:
				keyword_set["clickUv"] = '-'

			try:
				keyword_set["payConvRate"] = "%.2f%%" % (i['payConvRate'] * 100) #支付转化率
			except:
				keyword_set["payConvRate"] = '-'

			try:
				keyword_set["onlineGoodsCnt"] = i['onlineGoodsCnt'] # 在线商品数
				# onlineGoodsCnt = i['onlineGoodsCnt']  # 在内线商品数
			except:
				keyword_set["onlineGoodsCnt"] = '-'
				# onlineGoodsCnt = '-'
			# print (i['keyword'],tradeIndex,onlineGoodsCnt)
			# keyword_info.append(onlineGoodsCnt)
			keyword_info.append(keyword_set)
		# print (len(keyword_info))

		return keyword_info




### 测试

# 2018-6-4测试完成
if __name__ == '__main__':

	#############
	#### 滴滴优惠卷:像这种关键词是没有商品的，所以会取不到销量，只需要返回空值
	############

	get_data = Get_data()
	# get_data.cookies =  {'login': 'true', 'cookie17': 'VAYsVzijzewm', 't': '76cc542d2fdfeb79a44df9cc6a5b9d21', 'lgc': 'wedalaef', 'cna': '5dbzE7zfsnwCAW/myrh+5F+Z', 'csg': '244cacf3', 'isg': 'BBgYv2o694AMM9sEgJhgXIdI5U-qAXyL62qmOFIJZNMG7bjX-hFMGy5PIatdZjRj', 'cookie1': 'WvjsiM74d17DUMNtPpoSsB%2BnJxI63MrqPDKTTMRRkxM%3D', 'uss': '\"\"', 'ck1': '\"\"', 'cookie2': '1bf48bcb5dbe6a564e4339eb0595bbeb', 'tracknick': 'wedalaef', 'uc1': 'cookie16=UtASsssmPlP%2Ff1IHDsDaPRu%2BPw%3D%3D&cookie21=UIHiLt3xSixwH1aenGUFEQ%3D%3D&cookie15=VFC%2FuZ9ayeYq2g%3D%3D&existShop=true&pas=0&cookie14=UoTfL8JtXhHvqw%3D%3D&tag=8&lng=zh_CN', '_tb_token_': '31eee858e3e3', 'hng': '\"\"', '_nk_': 'wedalaef', 'uc3': 'vt3=F8dBzrtqBSz9Bgv3Trk%3D&id2=VAYsVzijzewm&nk2=FPzX%2F77X5Yw%3D&lg2=VT5L2FSpMGV7TQ%3D%3D', '_l_g_': 'Ug%3D%3D', 'skt': '5d9845aa1a428389', 'unb': '774063379'}
	get_data.cookies = {"uc1":"cookie16=WqG3DMC9UpAPBHGz5QBErFxlCA%3D%3D&cookie21=URm48syIZJfmZ9wa5H75FQ%3D%3D&cookie15=WqG3DMC9VAQiUQ%3D%3D&existShop=true&pas=0&cookie14=UoTYNc%2FqZemENg%3D%3D&tag=8&lng=zh_CN","_cc_":"V32FPkk%2Fhw%3D%3D","dnk":"%5Cu9879%5Cu5409%5Cu5BA3%5Cu5FD7","uc3":"vt3=F8dByR1WTajqxsle9Fk%3D&id2=UoTeO8tm%2Bu46&nk2=rORA76BF2Aw%3D&lg2=UIHiLt3xD8xYTw%3D%3D","_tb_token_":"3b16b8e4ed058","cookie2":"1b7d3d665f740433b83d5b23fefc8308","JSESSIONID":"B1907D1D0D6E49082362508A80E5FF80","cookie1":"W86v3ZpF1Fim3sQ9t3Qf%2FvpFpAecTCLJ2mheOXooPw8%3D","tg":"0","unb":"152569731","csg":"95a5d5bf","v":"0","hng":"CN%7Czh-cn%7CCNY","cna":"15ZiFBhnImkCAcFwhT7OdcJz","t":"5f58b5fb4b621a3fb4d55385abdd3f22","existShop":"MTU0MzQ4MTc2Mw%3D%3D","lgc":"%5Cu9879%5Cu5409%5Cu5BA3%5Cu5FD7","_nk_":"%5Cu9879%5Cu5409%5Cu5BA3%5Cu5FD7","isg":"BOjoQ4OhJ3R3wAvw-5DgfzBJtd_6EUwbG3rWqKIZNGNW_YhnSiEcq36_8RsNVgTz","sg":"%E5%BF%971f","cookie17":"UoTeO8tm%2Bu46","mt":"np=","_l_g_":"Ug%3D%3D","tracknick":"%5Cu9879%5Cu5409%5Cu5BA3%5Cu5FD7","skt":"4bbbd0f672a6359d"}

	# get_data.cookies = {'uc1': 'cookie14=UoTfL8Ju54UR7g%3D%3D&lng=zh_CN', 'tracknick': '', 'uc3': 'id2=&nk2=&lg2=', 'unb': '3966986640', 'csg': '519586be', 'isg': 'BKmphGy21qMaWOoX1nk23ixMtFwDdp2oMmU3C0ueJRDPEskkk8ateJeA0DgkkTXg', 'apusha5e68abda32918cbf95a295c9663849c': '%7B%22ts%22%3A1534815558917%2C%22parentId%22%3A1534815554084%7D', 'cookie2': '12afe0aff6e88c7e4df94ad7b46b20ac', 'sn': '%E6%AC%A3%E7%BE%8E%E9%9B%85%E6%97%97%E8%88%B0%E5%BA%97%3A%E8%BE%B0%E6%88%98', 'v': '0', '_tb_token_': '7ee3e373b974b', 'x': '2456118178', 'skt': '35dfc041431565e5', 't': '040a56a7b4e71e2f308f37067d7751a6', 'cna': '5dbzE7zfsnwCAW/myrh+5F+Z'}
	# 单线测试
	keyword = '牙膏架'

	print (get_data.get_onlineGoods(keyword))


	# 多线程测试
	# keyword_list = ['蜂王浆', '蜂王浆 纯天然', '蜂王浆 同仁堂', '蜂王浆胶囊', '蜂王浆冻干粉', '蜂王浆 纯天然 新鲜', '蜂王浆新鲜纯天然', '汪氏蜂王浆', '人参蜂王浆',
	# 				'蜂王浆同仁堂', '蜂王浆面膜', '人参蜂王浆口服液 正品',
	# 				'蜂王浆 自产', '蜂王浆口服液', '一叶子蜂王浆面膜', '蜂王浆 纯天然 同仁堂', '爱犬岛蜂王浆', '老山蜂王浆冻干粉', '蜂王浆 纯天然 新鲜 自产', '同仁堂蜂王浆',
	# 				'蜂王浆 正品']
	# pool = Pool(10)
	# data = pool.map(get_data.get_onlineGoods,keyword_list)
	# pool.close()
	# pool.join()
	# for item in data:
	# 	print(item)



	# print(keyword_list)
		
	# 多线程测试
	# keyword_list = ['蜂王浆', '蜂王浆 纯天然', '蜂王浆 同仁堂', '蜂王浆胶囊', '蜂王浆冻干粉', '蜂王浆 纯天然 新鲜', '蜂王浆新鲜纯天然', '汪氏蜂王浆', '人参蜂王浆',
	# 				'蜂王浆同仁堂', '蜂王浆面膜', '人参蜂王浆口服液 正品',
	# 				'蜂王浆 自产', '蜂王浆口服液', '一叶子蜂王浆面膜', '蜂王浆 纯天然 同仁堂', '爱犬岛蜂王浆', '老山蜂王浆冻干粉', '蜂王浆 纯天然 新鲜 自产', '同仁堂蜂王浆',
	# 				'蜂王浆 正品']


# 	# # # 获取销量
# 	pool = Pool(50)
# 	data = pool.map(get_data.get_sale,keyword_list)
# 	print(data)
# 	pool.close()
# 	pool.join()
	# for i in data:
	# 	print(i)

	# # 获取在线商品数(要登录淘宝)
	# pool = Pool(50)
	# data = pool.map(get_data.get_onlineGoods,keyword_list)
	# pool.close()
	# pool.join()
	# for i in data:
	# 	print i